package com.techventus.locations;



import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LocationsMenu extends ListActivity{
	  
	  String[] LOCATIONS = new String[] {
			    "Add Location","Elsewhere"
			  };
	  
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); 
//	 setContentView(R.layout.locationsmenu); 

	  Bundle bundle = getIntent().getExtras();
	  
	  
	  String[] locationsBundleAr =  bundle.getStringArray("locations");
	  
	  List<String> l = new ArrayList<String>();
	  l.add("\u2295 Add Location");
	  for(int i=0;i<locationsBundleAr.length;i++){
		 if(!locationsBundleAr.equals("Elsewhere")){
			 l.add(locationsBundleAr[i]);
		 }
	  }
	  l.add("Elsewhere");
	  
	  
	  
	  try{
		  String[] locations = l.toArray(new String[l.size()]);
			 
		  if(locations!=null)
			  LOCATIONS = locations;
	  }catch(Exception e){
		  e.printStackTrace();
	  }
	  setListAdapter(new ArrayAdapter<String>(this,
	          android.R.layout.simple_list_item_1, LOCATIONS));

	  getListView().setTextFilterEnabled(true);
	  
	  
	  
	}
	

	@Override
	protected void onResume(){
		super.onResume();
		Log.e("TECHVENTUS","RESUMING LOCATIONS MENU");
		resetLocations();
		  setListAdapter(new ArrayAdapter<String>(this,
		          android.R.layout.simple_list_item_1, LOCATIONS));

		  getListView().setTextFilterEnabled(true);
	}

	 
	void resetLocations(){
		SQLiteDatabase sql = openOrCreateDatabase("db",0,null);
		LOCATIONS = getStringArSQLVertical("SELECT locationName from LOCATIONS;",sql);
		SQLHelper.listPhoneAndLocationPhoneEnable(sql);
		sql.close();
	}
	


	private String[] getStringArSQLVertical(String query, SQLiteDatabase sql){
		String[] ret = new String[0];
		List<String> list = new ArrayList<String>();
		list.add("\u2295 Add Location");
		
		try{
			
			Cursor c = sql.rawQuery(query, null);
			if(c!=null){
				while(c.moveToNext()){
					if(!c.getString(0).equals("Elsewhere"))
					list.add(c.getString(0));
					Log.e("TECHVENTUS",c.getString(0));
				}
				c.close();
			}
			
		}catch(Exception o){
			o.printStackTrace();
		}
		
		list.add("Elsewhere");
		
		ret = list.toArray(new String[list.size()]);
		
		return ret;
	}
	
	 
	 @Override
	 protected void onListItemClick(ListView  l, View  v, int position, long id){
		// System.out.println((String) getListView().getItemAtPosition(position));
		
		 if(position == 0){
			Intent i = new Intent(this,NewLocation.class);
			
			startActivity(i);
		 }else{
			 Intent i = new Intent(this,LocationDecisionMenu.class);
			
			 Bundle bundle = new Bundle();
			 
			 bundle.putString("locationName", LOCATIONS[position]);
			 
			 Log.e("TECHVENTUS",bundle.getString("locationName"));
			//i.putExtra("locationName",LOCATIONS[position]);
			 i.putExtras(bundle);
			 
			 startActivity(i);
			 
			 finish();
		 }
		 
		// if(position == LOCATIONS.length-1){
			 
		// }
		 
		 
		 
		//System.out.println("Post");
	 }
}
/*
try{
Set<String> g = savedInstanceState.keySet();
List<String> locList = new ArrayList<String>();
locList.add("Add Location");
for(String loc:g){
	if(!loc.equals("Elsewhere")){
		locList.add(loc);
	}
}
locList.add("Elsewhere");
LOCATIONS = new String [locList.size()];
int i = 0;
for(String loc:locList){
	  LOCATIONS[i] = loc;
	  i++;
}

}catch(Exception e){
	  e.printStackTrace();
}
*/